package com.es.www.cglib.service.advice;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

/**
 * @Created by michangtao
 * @Date 2022/1/28 10:42
 * @Description
 */
@Slf4j
public class CglibProxy implements MethodInterceptor {

    //需要代理的目标对象
    private Object target;

    public CglibProxy(Object target) {
        this.target = target;
    }

    @Override
    public Object intercept(Object o, Method method, Object[] arr, MethodProxy methodProxy) throws Throwable {
        log.info("cglib动态代理，监听开始");
        return method.invoke(target,arr);
    }

    public Object getProxyObj(){
        Enhancer enhancer = new Enhancer();
        enhancer.setCallback(this);
        enhancer.setSuperclass(this.target.getClass());
        return enhancer.create();
    }
}
